package com.zmn.oms.admin.controller.report.coupanalyse;

import com.google.common.collect.Lists;
import com.zmn.common.dto.LayuiTableResponseDTO;
import com.zmn.dms.common.dto.coup.coupbatchstatistics.dro.CoupBatchStatisticsActivityDRO;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.oms.common.vo.KVDict;
import com.zmn.oms.model.entity.report.ReportCoupAnalyseQuery;
import com.zmn.oms.model.vo.report.ReportCoupAnalyseVO;
import com.zmn.oms.services.interfaces.report.ReportCoupAnalyseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 优惠卷分析报表controller
 * @author: HuangYongJie
 * @version: v1.0
 * @since: 2019/11/4 19:05
 **/
@Controller
@Slf4j
@RequestMapping("/report/coupanalyse/")
public class ReportCoupAnalyseListController extends ReportCoupAnalyseBaseController {

    @Autowired
    private ReportCoupAnalyseService reportCoupAnalyseService;

    /**
     * 分页查询
     *
     * @param query
     * @return
     */
    @RequiresPermissions("/zs/oms/report/coupanalyse/list.action")
    @ResponseBody
    @RequestMapping(value = "loadData")
    public LayuiTableResponseDTO loadData(ReportCoupAnalyseQuery query) {
        processingQueryConditions(query);
        List<ReportCoupAnalyseVO> reportCoupAnalyseVOS = reportCoupAnalyseService.listPageReportCoupAnalyse(query);
        LayuiTableResponseDTO<ReportCoupAnalyseVO> success = LayuiTableResponseDTO.success(query.getTotalCount(), reportCoupAnalyseVOS);
        success.setExtData(null);
        log.debug("reportCoupAnalyse result data:{}",reportCoupAnalyseVOS.toString());
        return success;
    }

    /**
     * 页面跳转
     *
     * @return
     */
    @RequiresPermissions("/zs/oms/report/coupanalyse/list.action")
    @RequestMapping("list")
    public ModelAndView companyList() {
        ModelAndView mv = new ModelAndView("report/coupanalyse/list");
        return mv;
    }

    /**
     * 导出
     *
     * @param query
     * @param response
     * @throws Exception
     */
    @RequiresPermissions("/zs/oms/report/coupanalyse/list.action")
    @GetMapping("export")
    public void export(ReportCoupAnalyseQuery query, HttpServletResponse response) throws Exception {
        LoginStaff currentStaff = getCurrentStaff();
        Integer exportLimit = currentStaff.getExportLimit();
        query.setPageSize(exportLimit);
        processingQueryConditions(query);
        Workbook workBook = reportCoupAnalyseService.exportExcel(query);
        workBook.write(response.getOutputStream());
    }

    /**
     * 活动列表
     *
     * @return
     */
    @GetMapping("getActivityList")
    @ResponseBody
    public List<KVDict> getActivityList() {
        List<CoupBatchStatisticsActivityDRO> dataList = findActivityList();
        if (dataList == null || dataList.isEmpty()) {
            return Lists.newLinkedList();
        }
        return dataList.stream().map(obj -> KVDict.<Integer>builder().value(obj.getActivityId()).text(obj.getActivityName() + "").build()).collect(Collectors.toList());
    }

    /**
     * 获取活动和会员产品关联的批次,取他们的交集作为查询条件
     *
     * @param query
     */
    public void processingQueryConditions(ReportCoupAnalyseQuery query) {
        List<Integer> batchIds1 = Lists.newArrayList();
        List<Integer> batchIds2 = Lists.newArrayList();
        List<Integer> batchIds = null;
        if (query.getActivityId() != null) {
            log.info("listBatchIdByActivity,activityId:{}", query.getActivityId());
            batchIds1 = listBatchIdByActivity(query.getActivityId());
            if (batchIds1 == null) {
                batchIds1 = Lists.newArrayList();
            }
            query.setBatchIds(batchIds);
        }
    }


}
